home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / Wood.0.72 / Sources / Utilities.m < prev    next >
Encoding:
Text File  |  1995-07-31  |  4.8 KB  |  196 lines

  1.  
  2.  
  3. #import <appkit/appkit.h>
  4.  
  5. #import "Global.h"
  6.  
  7. #define CHUNK 127
  8.  
  9. const char *localString(const char *aString)
  10. {
  11.        return NXLocalizedString(aString,NULL,aString);
  12. }
  13.  
  14. const char *appDirectory()
  15. {
  16.     static char appDirectory[MAXPATHLEN] = { '\0' };
  17.     char temp[MAXPATHLEN+1];
  18.     char *ptr = NULL;
  19.  
  20.     if(!appDirectory[0]) {
  21.         strcpy(appDirectory, NXArgv[0]);
  22.         if(ptr = rindex(appDirectory, '/')) *ptr = '\0';
  23.  
  24.         if (appDirectory && appDirectory[0]) {
  25.             getwd(temp);
  26.             chdir(appDirectory);
  27.             getwd(appDirectory);
  28.             chdir(temp);
  29.         }
  30. #ifdef DEBUG
  31.         printf("appDirectory = %s\n",appDirectory);
  32. #endif
  33.     }
  34.     return appDirectory;
  35. }
  36.  
  37. BOOL isThereSwapSpace(int imageSize)
  38. {
  39.     int rtn;
  40.     struct statfs stfs;
  41.  
  42.     rtn = statfs("/private/vm", &stfs);
  43.     if (rtn || ((stfs.f_bavail * stfs.f_bsize) < 0)) {
  44. #ifdef DEBUG
  45.         printf("NXApp: No space available on Hard Disk\n");
  46. #endif
  47.         NXRunAlertPanel(localString("Disk is Full"),
  48.                         localString("Enough swapspace is not available.  Please make more space by erasing old files or by rebooting the computer."),
  49.                         localString("Cancel"),NULL,NULL);
  50.         return NO;
  51.     } else if (rtn || ((stfs.f_bavail * stfs.f_bsize) < imageSize)) {
  52. #ifdef DEBUG
  53.         printf("NXApp: Not enough space available on Hard Disk\n");
  54.         printf("            Required size = %d Kbytes\n",imageSize/1024);
  55. #endif
  56.         rtn = NXRunAlertPanel(localString("Disk is Full"),
  57.                             localString("The swapfile is full.  Please make more space by erasing old files or by rebooting the computer.  Continuing may cause a crash."),
  58.                             localString("Cancel"),
  59.                             localString("Continue"),NULL);
  60.         if (rtn == NX_ALERTDEFAULT) return NO;
  61.         else                return YES;
  62.     }
  63.     return YES;
  64. }
  65.  
  66. char *stripnl(char *s)
  67. {
  68.     char *p;
  69.     for (p=s;*p;p++) if (*p == '\n' || *p == '\r') *p = '\0';
  70.     return s;
  71. }
  72.  
  73. char *execstr(char *s)
  74. /* Executes the passed string and returns a string value in that pointer */
  75. /* Stolen from Opener 3.0 */
  76. {
  77.     FILE *f = popen(s,"r");
  78.     char *p = s;
  79.     *s = '\0';
  80.     if (f){
  81.         while (fgets(p,256,f))
  82.             stripnl(p), p += strlen(p);
  83.         pclose(f);
  84.     }
  85.     return s;
  86. }
  87.  
  88. void fillMemory(unsigned char *ptr, int numBytes)
  89. {
  90.     int i;
  91.     for (i=0;i < numBytes; i++) {
  92.         *(ptr+i) = 0xff;
  93.     }
  94. }
  95.  
  96. /* Functions for creating and managing a dynamically-allocated fileList */
  97. /* - really just a list of strings.  They seem to be somewhat useful.   */
  98.  
  99. char **addFile(const char *file, char **list, int count, NXZone *zone)
  100. /* Adds the specified filename to the list of filenames.  It allocates 
  101.  * more memory in chunks as needed.
  102.  */
  103. {    
  104.     if (!list) list = (char **)NXZoneMalloc(zone,CHUNK*sizeof(char *));
  105.     list[count] = (char *)NXZoneMalloc(zone,(strlen(file)+1)*sizeof(char));
  106.     strcpy(list[count], file);
  107.     count++;
  108.     if (!(count% CHUNK)) {
  109.         list = (char **)NXZoneRealloc(zone,list,(((count/CHUNK)+1)*CHUNK)*sizeof(char *));
  110.     }
  111.     list[count] = NULL;
  112.     return list;
  113. }
  114.  
  115. void freeList(char **list)
  116. /* Frees the array of filenames */
  117.  {
  118.     char **strings;
  119.  
  120.     if (list) {
  121.         strings = list;
  122.         while (*strings) NX_FREE(*strings++);
  123.         NX_FREE(list);
  124.     }
  125. }
  126.  
  127.  
  128. NXRect *calcFrame(id printInfo, NXRect *viewRect)
  129. /*
  130.  * Calculates the size of the page the user has chosen minus its margins.
  131.  */
  132. {
  133.     float lm, rm, bm, tm;
  134.     const NXRect *paperRect;
  135.     float scale;
  136.  
  137.     viewRect->origin.x = viewRect->origin.y = 0.0;
  138.     paperRect = [printInfo paperRect];
  139.     scale = 1 / [printInfo scalingFactor];
  140.     [printInfo getMarginLeft:&lm right:&rm top:&tm bottom:&bm];
  141.     viewRect->size = paperRect->size;
  142.     viewRect->size.width -= lm + rm;
  143.     viewRect->size.height -= tm + bm;
  144.     viewRect->size.width *= scale;
  145.     viewRect->size.height *= scale;
  146.     return viewRect;
  147. }
  148.  
  149. void strtoRect(char * inputString, NXRect * frame)
  150. {
  151.     char    *start, *next;
  152.     
  153.     frame->origin.y = frame->origin.y = 0.0;
  154.     frame->size.width = frame->size.height = 0.0;
  155.     
  156.     start = inputString;
  157.     if ( *start == ' ' ) start++;
  158.     if ( *start == 'x' ) start++;
  159.     if ( *start == '\0' ) return;
  160.     frame->origin.x = strtod(start, &next);
  161.     if ( start == next ) return;
  162.  
  163.     start = next;
  164.     if ( *start == ' ' ) start++;
  165.     if ( *start == 'y' ) start++;
  166.     if ( *start == '\0' ) return;
  167.     frame->origin.y = strtod(start, &next);
  168.     if ( start == next ) return;
  169.     
  170.     start = next;
  171.     if ( *start == ' ' ) start++;
  172.     if ( *start == 'w' ) start++;
  173.     if ( *start == '\0' ) return;
  174.     frame->size.width = strtod(start, &next);
  175.     if ( start == next ) return;
  176.     
  177.     start = next;
  178.     if ( *start == ' ' ) start++;
  179.     if ( *start == 'h' ) start++;
  180.     if ( *start == '\0' ) return;
  181.     frame->size.height = strtod(start, &next);
  182.     if ( start == next ) return;
  183.     
  184.     return;    
  185. }
  186.  
  187. BOOL includesType(const NXAtom *types, NXAtom type)
  188. {
  189.     if (types)
  190.         while (*types)
  191.             if (*types++ == type)
  192.                 return YES;
  193.     return NO;
  194. }
  195.  
  196.